#include "noeud.h"

QImage Noeud::IMG_abstraction = QImage(":/images/images/nuage.png");
QImage Noeud::IMG_application = QImage(":/images/images/ordinateur.gif");
QImage Noeud::IMG_interaction = QImage(":/images/images/interaction.gif");
QImage Noeud::IMG_user = QImage(":/images/images/utilisateur.png");

QImage Noeud::IMG_orderIndependance= QImage(":/images/images/orderIndependance.png");
QImage Noeud::IMG_sequence = QImage(":/images/images/sequence.png");
QImage Noeud::IMG_synchronisation = QImage(":/images/images/synchronisation.png");
QImage Noeud::IMG_choix = QImage(":/images/images/choix.png");

unsigned int Noeud::COMPTEUR_ID = 0;

Noeud::Noeud(unsigned int categorie, QString n, QString description, unsigned int noLigne)
{
    this->monID         = Noeud::COMPTEUR_ID++;

    this->categorie     = categorie;
    this->type          = 0;
    this->nom           = n.append(QString::number(this->monID));
    this->description   = description;
    this->noLigne       = noLigne;
    this->pere          = 0;
    this->frere         = 0;
    this->reposition    = false;
    this->ariteOptionnel= false;
    this->ariteRepetable= false;
}

Noeud::~Noeud()
{
}

/*** GETTERS ET SETTERS ***/

unsigned int Noeud::getCategorie()
{
    return this->categorie;
}
unsigned int Noeud::getType()
{
    return this->type;
}
unsigned int Noeud::getFrequence()
{
    return this->frequence;
}
QString Noeud::getNom()
{
    return this->nom;
}
QString Noeud::getDescription()
{
    return this->description;
}
QString Noeud::getPrecondition()
{
    return this->precondition;
}
QString Noeud::getTempsMax()
{
    return this->tempsMax;
}
QString Noeud::getTempsMin()
{
    return this->tempsMin;
}
QString Noeud::getTempsMoy()
{
    return this->tempsMoy;
}
Noeud * Noeud::getPere()
{
    return this->pere;
}

QList<Noeud *> Noeud::getFils()
{
    return this->fils;
}
unsigned int Noeud::getNoLigne()
{
    return this->noLigne;
}
QPoint Noeud::getPosition()
{
    return this->position;
}
void Noeud::setCategorie(unsigned int categorie)
{
    this->categorie = categorie;
}
void Noeud::setType(unsigned int type)
{
    this->type = type;
}
void Noeud::setFrequence(unsigned int frequence)
{
    this->frequence = frequence;
}
void Noeud::setNom(QString nom)
{
    this->nom = nom;
}
void Noeud::setDescription(QString description)
{
    this->description = description;
}
void Noeud::setPrecondition(QString precondition)
{
    this->precondition = precondition;
}
void Noeud::setTempsMax(QString tempsMax)
{
    this->tempsMax = tempsMax;
}
void Noeud::setTempsMin(QString tempsMin)
{
    this->tempsMin = tempsMin;
}
void Noeud::setTempsMoy(QString tempsMoy)
{
    this->tempsMoy = tempsMoy;
}
void Noeud::setNoLigne(int noLigne)
{
    this->noLigne = noLigne;
}
void Noeud::setPere(Noeud * pere)
{
    this->pere = pere;
}
void Noeud::setPosition(QPoint p)
{
    if(p.rx() < 0)
    {
        p.setX(0);
    }
    if(p.ry() < 0)
    {
        p.setY(0);
    }
    this->position = p;
}

void Noeud::addFils(Noeud * fils)
{
    // on ajoute le fils
    this->fils.push_back(fils);

}

void Noeud::removeFils(Noeud * n)
{
    QList<Noeud *> l;
    foreach(Noeud * noeud, this->fils)
    {
        if(n != noeud)
        {
            l.push_back(noeud);
        }
    }

    this->fils = l;
}


QImage Noeud::getImage(unsigned int categorie)
{
    switch(categorie){

    case(Noeud::ABSTRACTION):
        return Noeud::IMG_abstraction;
        break;
    case(Noeud::APPLICATION):
        return Noeud::IMG_application;
        break;
    case(Noeud::INTERACTION):
        return Noeud::IMG_interaction;
        break;
    case(Noeud::USER):
        return Noeud::IMG_user;
        break;

    }

    return Noeud::IMG_abstraction;
}

QImage Noeud::getImageRelation(unsigned int relation)
{
    switch(relation){

    case(Noeud::ORDER_INDEPENDANCE):
        return Noeud::IMG_orderIndependance;
        break;
    case(Noeud::SEQUENTIAL_ENABLING):
        return Noeud::IMG_sequence;
        break;
    case(Noeud::SYNCHRONISATION):
        return Noeud::IMG_synchronisation;
        break;
    case(Noeud::CHOICE):
        return Noeud::IMG_choix;
        break;

    }

    return Noeud::IMG_orderIndependance;
}

void Noeud::remplirTypeComboBox(QComboBox *typeComboBox, int categorie)
{
    typeComboBox->clear();
    typeComboBox->setEnabled(true);
    typeComboBox->addItem("");
    //selon la categorie on rempli le champ type d apres la categorie
    switch(categorie){
    case(Noeud::ABSTRACTION):
        typeComboBox->setEnabled(false);
        break;
    case(Noeud::APPLICATION):
        typeComboBox->addItem("GENERATING_ALERTS");
        typeComboBox->addItem("GROUPING");
        typeComboBox->addItem("LOCATE");
        typeComboBox->addItem("OVERVIEW");
        typeComboBox->addItem("VISUALISE");
        typeComboBox->addItem("COMPARISON");
        typeComboBox->addItem("FEEDBACK");
        break;
    case(Noeud::INTERACTION):
        typeComboBox->addItem("RESPONDING_TO_ALERTS");
        typeComboBox->addItem("SELECTION_SINGLE_CHOICE");
        typeComboBox->addItem("SELECTION_MULTIPLE_CHOICE");
        typeComboBox->addItem("CONTROL");
        typeComboBox->addItem("EDIT");
        typeComboBox->addItem("MONITORING");
        break;
    case(Noeud::USER):
        typeComboBox->addItem("PLANNING");
        typeComboBox->addItem("PROBLEM_SOLVING");
        typeComboBox->addItem("COMPARING");
        break;

    }
}

// permet d'annuler une modification de noeud
void Noeud::inverserAvec(Noeud * aCopier)
{
    unsigned int i;
    QString s;
    QPoint p;

    i                   = aCopier->categorie;
    aCopier->categorie  = this->categorie;
    this->categorie     = i;

    i                   = aCopier->type;
    aCopier->type  = this->type;
    this->type     = i;

    i                   = aCopier->frequence;
    aCopier->frequence  = this->frequence;
    this->frequence     = i;

    s                   = aCopier->nom;
    aCopier->nom  = this->nom;
    this->nom     = s;

    s                   = aCopier->description;
    aCopier->description  = this->description;
    this->description     = s;

    s                   = aCopier->precondition;
    aCopier->precondition  = this->precondition;
    this->precondition     = s;

    s                   = aCopier->tempsMax;
    aCopier->tempsMax  = this->tempsMax;
    this->tempsMax     = s;

    s                   = aCopier->tempsMin;
    aCopier->tempsMin  = this->tempsMin;
    this->tempsMin     = s;

    s                   = aCopier->tempsMoy;
    aCopier->tempsMoy  = this->tempsMoy;
    this->tempsMoy     = s;

    p                  = aCopier->position;
    aCopier->position  = this->position;
    this->position     = p;
}

Noeud * Noeud::copie(Noeud * n)
{
    Noeud * noeud = new Noeud(n->categorie, n->nom, n->description, n->noLigne);
    noeud->monID = n->monID;
    noeud->nom = n->nom;
    noeud->type = n->type;
    noeud->frequence = n->frequence;
    noeud->precondition = n->precondition;
    noeud->tempsMax = n->tempsMax;
    noeud->tempsMin = n->tempsMin;
    noeud->tempsMoy = n->tempsMoy;
    noeud->pere = n->pere;
    noeud->fils = n->fils;
    noeud->position = n->position;
    noeud->ariteOptionnel = n->ariteOptionnel;
    noeud->ariteRepetable = n->ariteRepetable;
    noeud->relation = n->relation;

    return noeud;
}

// pour avoir une copie d'un sous arbre
// permettant d'etre cole par la suite
Noeud * Noeud::copieLocaleAvecFilsEtNouveauxID(Noeud * n)
{
    Noeud * noeud = new Noeud(n->categorie, n->nom, n->description, n->noLigne);
    noeud->type = n->type;
    noeud->frequence = n->frequence;
    noeud->precondition = n->precondition;
    noeud->tempsMax = n->tempsMax;
    noeud->tempsMin = n->tempsMin;
    noeud->tempsMoy = n->tempsMoy;

    return noeud;
}
